package thread;

public class ThreadDemo22 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(()-> {
           synchronized (A) {
               // sleep 是为了t2时间, 让t2也能拿到 B
               try {
                   Thread.sleep(1000);
               }catch (InterruptedException e) {
                   e.printStackTrace();
               }

               //尝试获取B, 并没有释放 A
               synchronized (B) {
                   System.out.println("t1拿到了两把锁");
               }
           }
        });

        Thread t2 = new Thread(()->{
            synchronized (A) {
                // sleep 是给t1时间, 让t1能拿到 A
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //尝试获取A, 并没有释放 B
                synchronized (B) {
                    System.out.println("t2拿到了两把锁");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
